var NewProd = function() {

	this.init = function() {
		
		$("body").delegate(".new_add_prod", "click", function(){
			
			var new_prodCate = $(this).parents(".new_prodCate");
			var pk_marbasclass = $(new_prodCate.find("[name='pkProdCl']")[0]).val();
			var pk_org = $("#vendorOrg").val();
			var pk_group = $("#vendorGroup").val();
			if( pk_org==null||pk_org==""){
				showCfmMsg(getLanMsg("0ec22180-100163"), getLanMsg("0ec22180-100165"));
				return false;
			}
			
			$("#newProdPicker").attr('source',pk_marbasclass); // 在newProdPicker中记录点击的来源
			var position = $(this).offset();   // 获取被点击元素的位置
			var picker_left = position.left - 652;
			var picker_top = position.top + 20;
			$("#newProdPicker .chooseAllProd").removeAttr("checked");
			var myts = new Date();
			$("#newProdPicker").find("#newProd_filter").attr("value","");
			var formStr = $("#newProdPicker").find("#newProd_filter").serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/qryEcProdByPkProdcl?pk_marbasclass="+pk_marbasclass+"&myts=" + myts + "&pk_org="+pk_org +"&pk_group="+pk_group,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
				
				    var prod_tree_list = $("#newProdPicker").find("[name='prod_tree_list']")[0];
				    $(prod_tree_list).empty();
					if (result != null && result != "[]") {
						var arr = eval(result);
						var existProds = new_prodCate.next().find("[name='newProdPk']");
						for(var loop=0; loop<arr.length; loop++){
							var obj = arr[loop];
							var prod_module = obj.prod_module==null ? "" : obj.prod_module;
							var prod_type = obj.prod_type==null ? "" : obj.prod_type;
							var cate = $("<div></div>").addClass("prod_tree_category");
							var inputCheckbox = $("<input></input>").attr("type", "checkbox").val(obj.pk_prod_v).css({"width":"20px"});
							var codeLabel = $("<label></label>").text(obj.prod_code).attr({"name":"code", "title":obj.prod_code});
							var labelProd = $("<label></label>").text(obj.prod_name).attr({"name":"prodname", "title":obj.prod_name}).css({"width":"534px"});
							var labelModule = $("<label></label>").text(prod_module).attr({"name":"prodmodule","title":prod_module}).css({"display":"none"});
							var labelType = $("<label></label>").text(prod_type).attr({"name":"prodtype","title":prod_type}).css({"display":"none"});
							var dproducedate = $("<input></input>").attr("name","dproducedate").attr("type","text").attr("class","datepicker");
							cate.append(inputCheckbox).append(codeLabel).append(labelProd)
								.append(labelModule).append(labelType).append(dproducedate);
							//过滤掉已经添加的产品
							if(existProds!= null && existProds.length !=0){
								for(var i=0; i<existProds.length; i++){
									if($(existProds[i]).val() == obj.pk_prod_v){
										break;
									}
								}
								if(i == existProds.length){
									$(prod_tree_list).append(cate);
								}
							}else{
								$(prod_tree_list).append(cate);
							}
							
						}
					}
				  
					$("#newProdPicker").css({'left':picker_left,'top':picker_top, 'width': '750px'}).slideDown("normal");
				}
			});
			return false;
		});
		
		
		$("body").delegate("#newFilterProd", "click", function(){
			
			var pk_marbasclass = $("#newProdPicker").attr('source');
			var new_prodCate = $("#newProdCateDivs").find('.new_prodCate').find("[value='"+pk_marbasclass+"']").parents(".new_prodCate");
			var pk_org = $("#vendorOrg").val();
			var pk_group = $("#vendorGroup").val();
			if( pk_org==null||pk_org==""){
				showCfmMsg(getLanMsg("0ec22180-100163"), getLanMsg("0ec22180-100165"));
				return false;
			}
			
			$("#newProdPicker .chooseAllProd").removeAttr("checked");
			var myts = new Date();
			var formStr = $("#newProdPicker").find("#newProd_filter").serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/qryEcProdByPkProdcl?pk_marbasclass="+pk_marbasclass+"&myts=" + myts + "&pk_org="+pk_org +"&pk_group="+pk_group,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
				
				    var prod_tree_list = $("#newProdPicker").find("[name='prod_tree_list']")[0];
				    $(prod_tree_list).empty();
					if (result != null && result != "[]") {
						var arr = eval(result);
						var existProds = new_prodCate.next().find("[name='newProdPk']");
						for(var loop=0; loop<arr.length; loop++){
							var obj = arr[loop];
							var prod_module = obj.prod_module==null ? "" : obj.prod_module;
							var prod_type = obj.prod_type==null ? "" : obj.prod_type;
							var cate = $("<div></div>").addClass("prod_tree_category");
							var inputCheckbox = $("<input></input>").attr("type", "checkbox").val(obj.pk_prod_v).css({"width":"20px"});
							var codeLabel = $("<label></label>").text(obj.prod_code).attr({"name":"code", "title":obj.prod_code});
							var labelProd = $("<label></label>").text(obj.prod_name).attr({"name":"prodname", "title":obj.prod_name});
							var labelModule = $("<label></label>").text(prod_module).attr({"name":"prodmodule","title":prod_module});
							var labelType = $("<label></label>").text(prod_type).attr({"name":"prodtype","title":prod_type});
							var dproducedate = $("<input></input>").attr("name","dproducedate").attr("type","text").attr("class","datepicker");
							cate.append(inputCheckbox).append(codeLabel).append(labelProd)
								.append(labelModule).append(labelType).append(dproducedate);
							//过滤掉已经添加的产品
							if(existProds!= null && existProds.length !=0){
								for(var i=0; i<existProds.length; i++){
									if($(existProds[i]).val() == obj.pk_prod_v){
										break;
									}
								}
								if(i == existProds.length){
									$(prod_tree_list).append(cate);
								}
							}else{
								$(prod_tree_list).append(cate);
							}
							
						}
					}
				  
				}
			});
			return false;
		});
		
		
		
		
		//确定
		$("body").delegate("#newProdPicked", "click", function(){
			
			var items = $("#newProdPicker .prod_tree_list").find(":checked");
			var desNew_prods = null;
			if (items != null && items.length > 0) {
				var desPk_marbasclass = $("#newProdPicker").attr('source');     //取得需要添加产品的目标产品分类div
				var desNew_prodCate_div = $("#newProdCateDivs").find("[value="+desPk_marbasclass+"]").parents(".new_prodCate_div");
					desNew_prods = desNew_prodCate_div.find(".new_prods");
					
				for (var loop = 0; loop < items.length; loop++) {
					var itemParent = $(items[loop]).parent();
					var tempcode = itemParent.find("[name='code']").text();
					var tempname = itemParent.find("[name='prodname']").text();
					var tempmodule = itemParent.find("[name='prodmodule']").text();
					var temptype = itemParent.find("[name='prodtype']").text();
					var tempdproducedate = itemParent.find("[name='dproducedate']").val();
					var temppk = $(items[loop]).val();
					
					var newrow = $("#newProdDescDemo").find(".new_prod").clone();
					
					newrow.find(".new_prod_name").text(tempcode +"  "+ tempname).attr("title",tempcode +"  "+ tempname);
					newrow.find(".new_prod_module").text(tempmodule).attr("title",tempmodule);
					newrow.find(".new_prod_type").text(temptype).attr("title",temptype);
					newrow.find(".new_produce_date").val(tempdproducedate);
					newrow.find("[name='newProdPk']").val(temppk);
					desNew_prods.append(newrow);
				}
				var newProdCate = desNew_prodCate_div.find(".new_prodCate");
				var pkProds = "";
				var dproducedates = "";
				var prods = desNew_prods.find(".new_prod");
				if(prods != null && prods.length != 0){
					for(var i=0; i<prods.length; i++){
						var currentPk = $(prods[i]).find("[name='newProdPk']").val();
						var currentDate = $(prods[i]).find("[name='newProduceDate']").val();
						if(currentDate == "" || currentDate == null){
							currentDate = "-";
						}
						pkProds += currentPk + ",";
						dproducedates += currentDate + ",";
					}
					newProdCate.find("[name='pkProdv']").val(pkProds.substring(0, pkProds.lastIndexOf(",")));
					newProdCate.find("[name='dproducedates']").val(dproducedates.substring(0, dproducedates.lastIndexOf(",")));
					newProdCate.find(".new_prod_num").text("(" + prods.length + ")");
					newProdCate.find("img").attr({'class':'dir_arrow','src':ebvp_context + '/commonres/common/frame/src/img/arrow_up_2.png'});
				}
			}
			$("#newProdPicker").slideUp();
			$("#newProdCateDivs").find(".new_prods:visible").slideUp(function(){
				$(this).parent().find("img").
				attr('src',ebvp_context + '/commonres/common/frame/src/img/arrow_down_2.png'); 
			});
			desNew_prods.slideDown("normal");
		});
		
		//全选产品
		$("body").delegate(".chooseAllProd", "click", function(){
			var isChecked = $(this).is(":checked");
			if(isChecked){
				$("#newProdPicker .prod_tree_list").find("[type='checkbox']").each(function(){
					$(this).attr("checked" , "checked");
				});
			}else{
				$("#newProdPicker .prod_tree_list").find("[type='checkbox']").each(function(){
					$(this).removeAttr("checked");
				});
			}
		});
		
		//取消
		$("body").delegate("#newProdCancel", "click", function(){
			$("#newProdPicker").slideUp("normal");
		});
		
		//删除产品
		$(".new_delete_prod").live('click', function() {
			var thisObj = $(this);
			var option = {
				title : getLanMsgs("0ec22180-100163"),
				content : getLanMsgs("0ec22180-100188"),
				confirm : function() {
				
					var newProdCateDiv = thisObj.parents(".new_prodCate_div");
					thisObj.parents(".new_prod").remove();
					var pkProds = "";
					var dproducedates = "";
					var prods = newProdCateDiv.find(".new_prod");
					if(prods != null && prods.length != 0){
						for(var i=0; i<prods.length; i++){
							var currentPk = $(prods[i]).find("[name='newProdPk']").val();
							var currentDate = $(prods[i]).find("[name='newProduceDate']").val();
							if(currentDate == "input time"){
								currentDate = "-";
							}
							pkProds += currentPk + ",";
							dproducedates += currentDate + ",";
						}
						newProdCateDiv.find("[name='pkProdv']").val(pkProds.substring(0, pkProds.lastIndexOf(",")));
						newProdCateDiv.find("[name='dproducedates']").val(dproducedates.substring(0, dproducedates.lastIndexOf(",")));
						newProdCateDiv.find(".new_prod_num").text("(" + prods.length + ")");
					}else{
						newProdCateDiv.find("[name='pkProdv']").val(pkProds);
						newProdCateDiv.find("[name='dproducedates']").val(dproducedates);
						newProdCateDiv.find(".new_prod_num").text("");
						newProdCateDiv.find(".new_prods").css('display','none');
						newProdCateDiv.find("img").attr({'class':'black_dot','src':ebvp_context + '/commonres/common/frame/src/img/list_img_dot_gray.png'});
					}
					return true;
				}
			}
			showConfirmDialog(option);
		});
		
	};
};

$(function() {
	var newProd = new NewProd();
	newProd.init();
	
});
